<%@ page import="com.bookstore.entity.Product" %>
<%@ page import="java.util.List" %>
<%@ page import="com.bookstore.factory.ServiceFactory" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/dashboard/">
    <%@ include file="../plugs.jsp"%>
    <title>在线书城后台管理系统</title>

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/css/ie10-viewport-bug-workaround.css"
          rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/examples/dashboard/dashboard.css"
          rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie8-responsive-file-warning.js"></script>
    <![endif]-->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false"
                    aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">在线书城后台管理系统</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">设置</a></li>
                <li><a href="#">退出</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="搜索">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <%@ include file="sideBar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">


            <h2 class="sub-header">商品列表</h2>
            <!-- 绑定模态框按钮 -->
            <button class="btn btn-primary" data-toggle="modal" data-target="#productModal" id="insertbtn">添加商品</button>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>商品序号</th>
                        <th>商品名称</th>
                        <th>商品价格</th>
                        <th>商品分类</th>
                        <th>商品数量</th>
                        <th>商品图片</th>
                        <th>商品描述</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        List<Product> products = (List<Product>) request.getSession().getAttribute("products");
                        if (products != null) {
                            for (int i = 0; i < products.size(); i++) {
                                Product product = products.get(i);
                    %>
                    <tr>
                        <td><%=i + 1%>
                        </td>
                        <td><%=product.getName()%>
                        </td>
                        <td><%=product.getPrice()%>
                        </td>
                        <td><%=product.getCategory()%>
                        </td>
                        <td><%=product.getPnum()%>
                        </td>
                        <td>
                            <img src=".<%=product.getImgurl()%>" style="width: 150px; height: 150px;" alt="..."
                                 class="img-thumbnail">
                        </td>
                        <td><%=product.getDescription()%>
                        </td>
                        <td>
                            <a class="btn btn-info"
                               data-toggle="modal" data-target="#productModal" id="updatebtn"
                               name="<%=product.getId()%>" getnum="<%=i%>">修改</a>
                            <a class="btn btn-danger" href="./manager.do?action=deleteProduct&id=<%=product.getId()%>">删除</a>
                        </td>
                    </tr>
                    <%
                            }
                        }
                    %>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-lg-5"></div>
        <div class="col-lg-6">
            <nav aria-label="Page navigation">
                <ul class="pagination pagination-lg">
                    <li>
                        <%
                            int start = 0;
                            int maxnum = 5;
                            int pagecount = 0;
                            int productsCount = Integer.parseInt(request.getSession().getAttribute("productsCount").toString());
                            if (productsCount % maxnum == 0) {
                                pagecount = productsCount / maxnum - 1;
                            } else {
                                pagecount = productsCount / maxnum;
                            }
                        %>
                        <a href="./manager.do?action=queryAllProduct&start=0&maxnum=<%=maxnum%>" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <%
                            int index1 = Integer.valueOf(request.getSession().getAttribute("index").toString());
                            if (index1 <= 1) {
                                index1 = 4;
                            } else {
                                index1--;
                            }
                        %>
                        <a href="./manager.do?action=queryAllProduct&start=<%=maxnum*index1-maxnum%>&maxnum=<%=maxnum%>"
                           aria-label="Previous">
                            <span aria-hidden="true"><span class="glyphicon glyphicon-menu-left"></span></span>
                        </a>

                    </li>
                    <%
                        for (int i = 0; i <= pagecount; i++) {
                    %>
                    <li><a href="./manager.do?action=queryAllProduct&start=<%=i*maxnum%>&maxnum=<%=maxnum%>"><%=i + 1%>
                    </a></li>
                    <%
                        }
                    %>

                    <%
                        int index2 = Integer.valueOf(request.getSession().getAttribute("index").toString());
                        if (index2 > 3) {
                            index2 = 1;
                        } else {
                            index2++;
                        }
                    %>
                    <li>
                        <a href="./manager.do?action=queryAllProduct&start=<%=maxnum*index2-maxnum%>&maxnum=<%=maxnum%>"
                           aria-label="Next">
                            <span aria-hidden="true"><span class="glyphicon glyphicon-menu-right"></span></span>
                        </a>
                    </li>
                    <li>

                        <a href="./manager.do?action=queryAllProduct&start=<%=pagecount*maxnum%>&maxnum=<%=maxnum%>"
                           aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="row">
                <div class="col-lg-6">共<%=productsCount%>条记录</div>
                <div class="col-lg-6">当前是<%=Integer.valueOf(request.getSession().getAttribute("index").toString())%>
                    页，共<%=pagecount + 1%>页
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-1"></div>
</div>

<!-- 商品模态框部分 -->
<!-- Modal -->


<div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="updateclosemodel"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">商品修改</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-1"></div>
                    <div class="col-md-10">
                        <form class="form-horizontal" method="post" action="./manager.do">
                            <input type="hidden" name="action" value="updateProduct">
                            <input type="hidden" name="id">
                            <div class="form-group">
                                <label for="productName" class="col-sm-3 control-label">商品名称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productName" name="productName"
                                           placeholder="商品名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productPrice" class="col-sm-3 control-label">商品价格</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productPrice" name="productPrice"
                                           placeholder="商品价格">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="categary" class="col-sm-3 control-label">商品分类</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="categary" name="categary"
                                           placeholder="商品分类">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productNum" class="col-sm-3 control-label">商品数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="productNum" name="productNum"
                                           placeholder="商品数量">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productImg" class="col-sm-3 control-label">商品图片</label>
                                <div class="col-sm-9">
                                    <input type="file" class="form-control" id="productImg" name="productImg"
                                           placeholder="请上传商品图片" changesnum="0">
                                    <input type="hidden" id="imgpath" name="imgpath" value="">
                                    <div class="row">
                                        <div class="col-xs-12 col-md-12">
                                            <a href="#" class="thumbnail">
                                                <img src="" id="imgChange" height="95" width="100"
                                                     style="display: none">
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="productDes" class="col-sm-3 control-label">商品描述</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" id="productDes" name="productDes" placeholder="商品描述"
                                    ></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-1"></div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary" id="btnsave">保存</button>
            </div>
        </div>
    </div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
        crossorigin="anonymous"></script>
<script>
    window.jQuery || document.write(
        '<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/jquery.min.js"><\/script>'
    )
</script>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/js/bootstrap.min.js"></script>
<!-- Just to make our placeholder images work. Don't actually copy the next line! -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/holder.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie10-viewport-bug-workaround.js"></script>
<script type="text/javascript" src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script>
    $(function () {
        var numclick = 0;
        $('#productImg').change(function () {
            if (numclick == 1) {
                numclick = 0;
                $('#imgChange').css("display", "block");
                return;
            }
            numclick = 1;

            var formData = new FormData();
            formData.append('productImg', $("#productImg")[0].files[0]);
            $.ajax({
                async: true,
                url: "./uploadImgs.do",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    var date1 = JSON.parse(data);
                    var pathimg = "./uploadImg/" + date1;
                    $('#imgChange').attr("src", "." + pathimg);
                    $('#imgChange').css("display", "block");
                    $('#imgpath').val(pathimg);
                },
                error: function (data1) {
                    $('#imgChange').attr("src", "");
                    $('#imgChange').css("display", "none");
                    errdata = JSON.parse(data1);
                    alertFun(data1, "上传");
                },
                dataType: "text"
            });
        });
        $('div').on('click', '#insertbtn', function () {
            $('#myModalLabel').text("商品添加");
            $('#productName').val("");
            $('#productPrice').val("");
            $('#categary').val("");
            $('#productNum').val("");
            $('#productDes').val("");
            $('#imgChange').attr("src", "");
            $('#imgChange').css("display", "none");
        })
        $('td').on('click', '#updatebtn', function () {
            $('#updateclosemodel')[0].class = 'close';
            console.log(this);
            var numup = $(this).attr('getnum');
            var $data1Arr = $("tbody tr:eq(" + numup + ")").children();
            $('#myModalLabel').text("商品修改");
            $('#productName').val($data1Arr[1].innerText);
            $('#productPrice').val($data1Arr[2].innerText);
            $('#categary').val($data1Arr[3].innerText);
            $('#productNum').val($data1Arr[4].innerText);
            $('#imgChange').attr("src", $($data1Arr[5]).children()[0].src);
            $('#imgChange').css("display", "block");
            $('#imgpath').val($($data1Arr[5]).children()[0].src);
            $('#productDes').val($data1Arr[6].innerText);
            $('#updateclosemodel')[0].class = 'open';
        })
        $('#btnsave').click(function () {
            var caozuo = $('#myModalLabel').text();
            var name = $('#productName').val();
            var price = $('#productPrice').val();
            var category = $('#categary').val();
            var pnum = $('#productNum').val();
            var imgurl = $('#imgpath').val();
            var description = $('#productDes').val();
            var action = 'updateProduct';
            var id = $('#updatebtn')[0].name;
            if ("商品添加" == caozuo) {
                action = "addProduct";
            }
            console.log(caozuo);
            console.log(action);
            $.ajax({
                async: true,
                url: "./manager.do",
                type: "POST",
                data: {
                    action: action,
                    id: id,
                    name: name,
                    price: price,
                    category: category,
                    pnum: pnum,
                    imgurl: imgurl,
                    description: description
                },
                success: function (data) {
                    if (data == null || data == "" || data == undefined) {
                        $('#updateclosemodel')[0].class = close;
                        return;
                    }
                    var data1 = JSON.parse(data);
                    if (data1 == 1) {
                        $('#updateclosemodel')[0].class = close;
                    }
                    setTimeout(alertFun(data1, caozuo.substring(2, 4)), 100);
                    location.href = "./manager.do?action=queryAllProduct";
                },
                dataType: "text"
            });
        });

    })

    function alertFun(num, txt) {
        if (num == 0) {
            Swal.fire({
                type: 'error',
                title: '失败提示.',
                text: txt + '失败!',
                footer: '<a href>请检输入数据是否正确</a>'
            })
        } else {
            Swal.fire({
                position: 'top-middle',
                type: 'success',
                title: txt + '成功！',
                showConfirmButton: true,
                timer: 2500
            })

        }
    }
</script>
</body>

</html>
